(function () {
    if (localStorage.getItem("token")!=="" && localStorage.getItem("token")!==null){
        location.href = encodeURI("waitPage.html")
    }else if(sessionStorage.getItem("token")!=="" && sessionStorage.getItem("token")!==null){
        location.href = encodeURI("waitPage.html")
    }
})()

$("#loginBtn").click(function () {

    var rememberMe = 0;
    if ($("#rememberMe")[0].checked){
        rememberMe = 1;
    }

    $.ajax({
        url: "/user/login",
        type: "POST",
        dataType: 'json',
        headers: {"Content-Type": "application/json;charset=utf-8"},
        data: JSON.stringify({
            "userName": $("#userName").val(),
            "userPassword": $("#userPassword").val(),
            "rememberMe": rememberMe
        }),
        success(data){
            if (data.code===200){
                if (rememberMe === 1){
                    localStorage.setItem("rememberMe","1")
                    localStorage.setItem("token",data.data.token)
                    localStorage.setItem("userName",data.data.userName)
                    localStorage.setItem("userIdP",data.data.userId)
                    localStorage.setItem("usernameLabel",$("#userName").val())
                }else{
                    localStorage.setItem("rememberMe","0")
                    sessionStorage.setItem("token",data.data.token)
                    sessionStorage.setItem("userName",data.data.userName)
                    sessionStorage.setItem("userIdP",data.data.userId)
                    sessionStorage.setItem("usernameLabel",$("#userName").val())
                }
            }

            location.href = encodeURI("index.html")
        },error(){
            alert("账号或密码输入有误请重试！")


        }
    })
})
